import { _getUserInfo } from './common/api/signin'
import { loading, toast } from './utils/util'
App({
  store: {
    userinfo: null
  },
  getInfoByMemberid (memberid) {
    console.log('getInfoByMemberid...')
    return _getUserInfo(memberid)
  },
  launch () {
    let memberid = wx.getStorageSync('memberid')
    return new Promise((resolve, reject) => {
      if (!memberid) {
        // 本地没有memberid，跳转登入
        reject('本地没有memberid，跳转登入')
        wx.showModal({
          title: '对不起',
          content: '您还未登入，请先登入',
          showCancel: false,
          success: r => {
            if (r.confirm) {
              wx.navigateTo({
                url: '/pages/signin/index'
              })
            }
          }
        })
      } else {
        // 本地有memberid，请求会员信息
        this.getInfoByMemberid(memberid).then(res => {
          if (res.data.IsSuccess) {
            let {
              Name: name,
              Tel: tel,
              City: city,
              DepartMent: depart,
              ID: id
            } = res.data.Data
            this.store.userinfo = { name, tel, city, depart }
          }
          resolve(this.store.userinfo)
        }).catch(err => {
          reject(err)
          wx.showModal({
            title: '对不起',
            content: '获取信息失败，请重新登入',
            showCancel: false,
            success: r => {
              if (r.confirm) {
                wx.navigateTo({
                  url: '/pages/signin/index'
                })
              }
            }
          })
        })
      }
    })
  },
  loading,
  toast,
  onLaunch (res) {
  }
})